import devicePieChart from './device-pie-chart.vue'
import deviceUseChart from './device-use-chart.vue'
import orderChart from './order-chart.vue'
import abnormalBlock from './abnormal-block.vue'
import mapChart from './map.vue'
import storeModal from '../store-modal.vue'
import siteModal from '../site-modal.vue'

import countTo from 'vue-count-to'

import topHeader from '../top-header.vue'
import deviceOnlineRateChart from '../device-online-rate.vue'

import { Request } from '@/utils'

export default {
    data () {
        return {
            mapData : {
                storeList : [], //门店列表
                siteList : [], //站点列表
                deviceList : [], //设备列表
            },

            completePaddintTop : false,
            yunyingTime : 0,
            currentStoreId : 0,
            currentSiteId : 0,
            showStoreModal : false,
            showSiteModal : false,
            storeModalLeft : 0,
            storeModalTop : 0,

            deviceTotalCount : 0, //设备总数
            deviceCategoryData : null, //设备类型
            deviceOnlineRateData : null, //设备在线率
            deviceLatelyUseData : null, //设备近七天使用
            todayOrderCount : 0, //今日订单总数
            latelyOrderData : null, //近七天订单和金额

            exceptionDataList : [], //异常
        }
    },

    components : {
    topHeader,
        devicePieChart,
        deviceUseChart,
        orderChart,
        abnormalBlock,
        mapChart,
        storeModal,
        siteModal,
        deviceOnlineRateChart,

        countTo
    },

    methods : {
        mouseoverMap (e) {
            if(e.value) {
                var id = e.value[2]
                var type = e.value[3]
                if(type == 2) { //门店
                    this.currentStoreId = id
                    this.showStoreModal = true
                }else if(type == 3) { //站点
                    this.currentSiteId = id
                    this.showSiteModal = true
                }else if(type == 3) { //设备

                }

                
                this.$nextTick(() => {
                    var WH = document.documentElement.offsetHeight || document.body.offsetHeight
                    var H = this.$refs.storeModal.offsetHeight
                    var clientY = e.clientY
                    
                    if(clientY < H) {
                        clientY += (H - clientY + 10)
                    }

                    this.storeModalLeft = e.clientX
                    this.storeModalTop = clientY - H
                })
                
            }else {
                this.showStoreModal = false
                this.showSiteModal = false
            }
            
        },

        run () {

            //获取运营时间
            (new Request()).get('statistic/device/getOperateInfo', res => { //运营时间
                this.yunyingTime = res.data.operateTime || 0
            });

            (new Request()).get('statistic/device/totalDeviceNum', res => { //获取设备数
                this.deviceTotalCount = res.data || 0
            });

            (new Request()).get('statistic/device/totalDeviceByVehicleType', res => { //获取设备
                var deviceCategoryData = []
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        deviceCategoryData.push({name : this.$store.state.vehicleTypeList[item.vehicleType], value : item.deviceNum})
                    })
                }
                this.deviceCategoryData = deviceCategoryData
            });


            (new Request()).get('statistic/device/totalDeviceOnlineNum', res => { //获取设备在线率
			    var deviceOnlineRateData = []
			    if(res.success && res.data && res.data.length > 0) {
			        res.data.forEach(item => {
			            deviceOnlineRateData.push({name : this.$store.state.vehicleTypeList[item.vehicleType], value :  ((item.onlineNum || 0) / item.deviceNum).toFixed(1)})
			        })
			    }
			    this.deviceOnlineRateData = deviceOnlineRateData
			});

            (new Request()).get('statistic/device/totalSevenDaysDeviceUseNum', res => { //获取设备近七天使用次数
                var deviceLatelyUseData = {
                    date : [],
                    data : [
                        {'name' : '电单车', data : []},
                        {'name' : '充电柜', data : []},
                        {'name' : '换电柜', data : []},
                        {'name' : '汽车', data : []},
                    ]
                }
    
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        deviceLatelyUseData.date.push(item.date)
                        deviceLatelyUseData.data[0].data.push(item.electrocar)
                        deviceLatelyUseData.data[1].data.push(item.chargeCabinet)
                        deviceLatelyUseData.data[2].data.push(item.exchangeCabinet)
                        deviceLatelyUseData.data[3].data.push(item.car)
                    })
                }
                this.deviceLatelyUseData = deviceLatelyUseData
            });

            (new Request()).get('statistic/order/todayOrders', res => { //获取今日订单数
                this.todayOrderCount = res.data.todayOrders || 0
            });

            (new Request()).get('statistic/order/totalSevenDaysChargeOrderNum', res => { //获取近七天订单和金额

                var latelyOrderData = {
                    date : [],
                    order : [],
                    amount : []
                }
    
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        latelyOrderData.date.push(item.date)
                        latelyOrderData.order.push(item.orderNum)
                        latelyOrderData.amount.push(item.amount)
                    })
                }
    
                this.latelyOrderData = latelyOrderData
    
            });

            (new Request()).get('statistic/order/totalChargeOrderException', res => { //充电异常
                var exceptionDataList = []
                if(res.success && res.data) {
                    res.data.forEach(item => {
                        exceptionDataList.push({
                            type : item.exceptionType,
                            rate : item.totalNum <= 0 ? 0 : (item.exceptionNum / item.totalNum * 100).toFixed(1),
                            data : [{name : '总共', value : item.exceptionNum}, {name : '未处理', value : item.untreatedNum}, {name : '已处理', value : item.handledNum}]
                        })
                    })
                }

                this.exceptionDataList = exceptionDataList
            });


            if(this.$store.getters.isAgent()) {
                (new Request()).get('statistic/device/getStoreList', res => { //获取门店
                    try {
                       if(res.success && res.data) {
                           var storeList = []
                           res.data.forEach(item => {
                                let longitude = Number(item.longitude || 0)
                                let latitude = Number(item.latitude || 0)
                                if(!longitude || !latitude) return
                                storeList.push({
                                    longitude,
                                    latitude,
                                    name : item.name,
                                    id : item.id
                                })
                           })
                           this.mapData.storeList = storeList
                       }
                    } catch (error) {
    
                    }
                });
            }

            if(this.$store.getters.isAgent() || this.$store.getters.isStore()) {
                (new Request()).get('statistic/device/getSiteList', res => { //获取站点
                    if(res.success && res.data) {
                        var siteList = []
                        res.data.forEach(item => {
                            var longitude = Number(item.longitude || 0)
                            var latitude = Number(item.latitude || 0)
                            if(isNaN(longitude) || isNaN(latitude) || !longitude || !latitude) return
                            siteList.push({longitude, latitude, name : item.name, id : item.id})
                        })
                        this.mapData.siteList = siteList
                    }
                });
            }


            (new Request()).get('statistic/device/getStatisticDeviceList', res => { //获取设备
                if(res.success && res.data) {
                    var deviceList = []
                    res.data.forEach(item => {
                        var longitude = Number(item.longitude || 0)
                        var latitude = Number(item.latitude || 0)
                        if(isNaN(longitude) || isNaN(latitude) || !longitude || !latitude) return
                        deviceList.push({longitude, latitude, name : item.deviceName, deviceNo :item.deviceNo, id : item.id})
                    })
                    this.mapData.deviceList = deviceList
                }
            });

            


            setTimeout(() => {
                this.run()
            }, 30 * 60 * 1000)

        }
    },

    mounted () {
        this.$refs.container.style.paddingTop = this.$refs.header.offsetHeight + 'px';
        this.completePaddintTop = true;

        this.run()

    }
}